package com.hboxs.asl.controller.app.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.FinancialRecord;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.FinancialRecordService;
import com.hboxs.asl.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 我的关注
 */
@Controller(value = "appFinancialRecord")
@RequestMapping(value = "/app/m/financialRecord")
public class FinancialRecordController extends BaseController {

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "financialRecordServiceImpl")
    private FinancialRecordService financialRecordService;

    /**
     * 获取我的余额
     * @return
     */
    @RequestMapping(value = "/my_balance", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage myBalance() {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            return JsonMessage.success(
                    "balance", member.getBalance()
            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 获取我的积分
     * @return
     */
    @RequestMapping(value = "/my_point", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage myPoint() {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            return JsonMessage.success(
                    "point", member.getPoint()
            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 支出记录
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/expenditureRecord", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage expenditureRecord(Pageable pageable) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Page<FinancialRecord> page = financialRecordService.expenditureRecord(pageable, member);
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "balance", member.getBalance(),

                    "point", member.getPoint(),

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "amount", "sn", "sourse", "createDate", "createDateF"
                    )
            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 收入记录
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/incomeRecord", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage incomeRecord(Pageable pageable) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Page<FinancialRecord> page = financialRecordService.incomeRecord(pageable, member);
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "balance", member.getBalance(),

                    "point", member.getPoint(),

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "amount", "sn", "sourse", "createDate", "createDateF"
                    )
            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }
















}
